package com.qf.vip.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.vip.entity.Users;
import com.qf.vip.mapper.UsersMapper;
import com.qf.vip.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(Users users) {
        int i = usersMapper.updateByPrimaryKeySelective(users);
        if(i!=1){
            System.out.println("【修改会员信息】修改失败！");
            throw new RuntimeException("【修改会员信息】修改失败！");
        }
    }

    @Override
    public Users findByNameAndPassword(String name, String password) {
        return usersMapper.findByNameAndPassword(name,password);
    }

    @Override
    public PageInfo<Users> findAllByPage(Integer page, Integer rows) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<Users> usersList=usersMapper.findByName(null);

        //3、封装PageInfo
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);

        //4、返回
        return pageInfo;
    }

    @Override
    public PageInfo<Users> findByNameAndPage(Integer page, Integer rows, String name) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<Users> usersList=usersMapper.findByName(name);

        //3、封装PageInfo
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);

        //4、返回
        return pageInfo;
    }
}
